package com.chen.stopwatch.views;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.chen.stopwatch.R;
import com.chen.stopwatch.common.IToolBarClickedListener;
import com.chen.stopwatch.utils.Utils;

public class ToolBar extends ViewGroup {
	public static final int TOOl_BAR_ITEM_SHARE = 0;

	public static final int TOOL_BAR_ITEM_SCREEN = 1;

	public static final int TOOL_BAR_ITEM_HISTORY = 2;

	public static final int TOOL_BAR_ITEM_VOLUME = 3;

	public static final int TOOL_BAR_ITEM_COUNT = 4;

	public static final String KEY_VOLUME = "share";

	public static final String KEY_SCREEN = "screen";

	ArrayList<ToolBarItem> _toolBarItems = null;

	IToolBarClickedListener mToolBarClickedListener = null;

	Context _context = null;

	boolean _neededUpdateToolBar = false;

	public ToolBar(Context context) {
		super(context);
		init(context);
	}

	public ToolBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public ToolBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public void setClickedLis(IToolBarClickedListener lis) {
		mToolBarClickedListener = lis;
	}

	public void setScreenOn(boolean on) {
		Utils.sharedPreferences(_context, KEY_SCREEN, on);

		ToolBarItem item = getToolBarItemById(TOOL_BAR_ITEM_SCREEN);
		if (null == item)
			return;

		item.setDrawableRes(on ? R.drawable.btn_screen_on
				: R.drawable.btn_screen_off);
	}

	public void setSoundOn(boolean volume) {
		Utils.sharedPreferences(_context, KEY_VOLUME, volume);

		ToolBarItem item = getToolBarItemById(TOOL_BAR_ITEM_VOLUME);
		if (null == item)
			return;

		item.setDrawableRes(volume ? R.drawable.btn_volume_on
				: R.drawable.btn_volume_off);
	}

	public void setEnableShare(boolean enable) {
		ToolBarItem item = getToolBarItemById(TOOl_BAR_ITEM_SHARE);
		item.getButton().setEnabled(enable);
		// if (enable) {
		// if (null != item)
		// return;
		//
		// ToolBarItem newItem = new ToolBarItem(_context);
		// this.addView(newItem.getButton());
		// newItem.getButton().setOnClickListener(mOnClickListener);
		// newItem.setItemId(TOOl_BAR_ITEM_SHARE);
		// newItem.setDrawableRes(R.drawable.btn_share);
		// newItem.setTextId(R.string.share);
		// _toolBarItems.add(0, newItem);
		// } else {
		// if (null == item)
		// return;
		//
		// this.removeView(item._button);
		// _toolBarItems.remove(item);
		// }
		// _neededUpdateToolBar = true;
		// invalidate();
	}

	public ToolBarItem getToolBarItemById(int id) {
		if (null == _toolBarItems)
			return null;

		int size = _toolBarItems.size();
		for (int i = 0; i < size; i++) {
			ToolBarItem item = _toolBarItems.get(i);
			if (null == item)
				continue;

			if (item.getItemId() == id)
				return item;
		}

		return null;
	}

	void init(Context context) {
		_toolBarItems = new ArrayList<ToolBarItem>();
		_context = context;
		for (int i = 0; i < TOOL_BAR_ITEM_COUNT; ++i) {
			ToolBarItem item = new ToolBarItem(_context);
			this.addView(item.getButton());
			item.getButton().setOnClickListener(mOnClickListener);
			// item.getTextView().setGravity(Gravity.CENTER | Gravity.TOP);
			_toolBarItems.add(item);
		}

		_toolBarItems.get(0).setItemId(TOOl_BAR_ITEM_SHARE);
		_toolBarItems.get(0).setDrawableRes(R.drawable.btn_share);
		_toolBarItems.get(1).setItemId(TOOL_BAR_ITEM_SCREEN);
		_toolBarItems.get(1).setDrawableRes(R.drawable.btn_screen_on);
		_toolBarItems.get(2).setItemId(TOOL_BAR_ITEM_VOLUME);
		_toolBarItems.get(2).setDrawableRes(R.drawable.btn_volume_on);

		_toolBarItems.get(3).setItemId(TOOL_BAR_ITEM_HISTORY);
		_toolBarItems.get(3).setDrawableRes(R.drawable.btn_history);

		_toolBarItems.get(0).setTextId(R.string.share);
		// _context.getResources().getString(R.string.)
		_toolBarItems.get(1).setTextId(R.string.screen);
		_toolBarItems.get(2).setTextId(R.string.sound);
		_toolBarItems.get(3).setTextId(R.string.btn_switch_to_history);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		if (!changed && !_neededUpdateToolBar)
			return;

		int size = _toolBarItems.size();

		float cell = (r - l) / size;
		for (int i = 0; i < size; ++i) {
			ToolBarItem item = _toolBarItems.get(i);
			if (null == item || null == item.getButton())
				continue;

			int toolBarItemL = l + (int) (i * cell);
			int toolBarItemR = l + (int) ((i + 1) * cell);

			int toolBarItemT = t;
			int toolBarItemB = b;
			item.getButton().layout(
				toolBarItemL, toolBarItemT, toolBarItemR, toolBarItemB);
		}
		if (_neededUpdateToolBar)
			_neededUpdateToolBar = false;
	}

	OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			int itemId = -1;
			if (null != mToolBarClickedListener)
				mToolBarClickedListener.toolBarItemClicked(((ToolBarItem) v.getTag()).getItemId());
		}
	};
}
